package com.Jhu1.controller;

import com.Jhu1.entity.Student;
import com.Jhu1.service.IStudentService;
import com.Jhu1.util.PageResult;
import com.Jhu1.util.R;
import com.Jhu1.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zjh
 * @since 2025-01-07
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private IStudentService studentService;
    @RequestMapping("/list")
    public R getStudent(){
        return R.ok().data("list",studentService.list());
    }
    //分页
    @GetMapping("/findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable("page") Integer page,
                        @PathVariable("pageSize") Integer pageSize) {
        PageResult<Student> pr = studentService.findByPage(page,pageSize);
        return R.ok().data("pageResult",pr);
    }
    //条件查询加上分页
    @PostMapping("/searchStudent/{page}/{pageSize}")
    public R searchStudent(@PathVariable("page") Integer page,
                    @PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) StudentVO vo){
        PageResult<Student> pageResult = studentService.searchStudent(page,pageSize,vo);
        return R.ok().data("pageResult",pageResult);
    }
    @PostMapping("/addStudent")
    public R addStudent(@RequestBody Student student){
        System.out.println("!111");
        boolean flag = studentService.save(student);
        System.out.println("2222");
        return flag ? R.ok().message("添加学生成功!") : R.error().message("添加学生失败!");
    }

    @PutMapping("/updateStudent")
    public R updateStudent(@RequestBody Student student){
        boolean flag = studentService.updateById(student);
        return flag ? R.ok().message("修改学生成功!") : R.error().message("修改学生失败!");
    }
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") String ids){  // 1,2,3
        String[] split = ids.split(",");
        List<String> sids = Arrays.asList(split);
        return studentService.removeByIds(sids) ? R.ok().message("删除学生成功!") : R.error().message("删除学生失败!");
    }
}
